#define quickread
#ifdef quickread
#include <cstdio> 
template <typename T>
inline void read(T& x)
{
    int c=getchar(), f=1; x=0;
    while(c<'0'||'9'<c) {if(c=='-') f=-1; c=getchar();}
    while('0'<=c&&c<='9') 
        x=(x<<3)+(x<<1)+c-'0', c=getchar();
    x*=f;
}
template <typename T>
inline void quickwrite(T x)
{
    if(x<0) putchar('-'), x*=-1;
    if(x>=10) quickwrite(x/10);
    putchar(x%10+'0');
}
#else 
#include <iostream>
#endif
#include <cmath>
using namespace std;
#define DEBUG

/*
* 1.u==v ans=0
* 1.若u,v互成倍数 ans=max(u, v)
* 2.若u,v不互成倍数，ans=u+v
*/

int q;
void init()
{
    read(q);
}

void solve()
{
    init();
    while(q--)
    {
        long long u, v; read(u), read(v);
        if(u==v) quickwrite(0);
        else if(u%v==0||v%u==0) quickwrite(max(u, v));
        else quickwrite(u+v);
        puts("");
    }
}

// #undef DEBUG
signed main()
{
    #ifdef DEBUG
        freopen("./in.txt", "r", stdin);
        freopen("./out.txt", "w", stdout);
    #endif
    #ifndef quickread
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    #endif

    int T=1; //scanf("%d", &T);
    while(T--) 
    {
        solve();
    }
    return 0;
}
